﻿
Imports osi.root.constants
Imports osi.root.delegates
Imports osi.root.template
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils
Imports osi.service.argument
Imports osi.service.commander
Imports osi.service.storage
Imports osi.service.tcp
Imports osi.service.http
Imports ss = osi.service.storage
Imports questioner = osi.service.commander.questioner

Public Module _allocator
    Sub New()
        binder(Of _do_val_ref_ref(Of var, questioner, String, Boolean), 
                  allocate_http_questioner_binder_protector).set_global(AddressOf allocate_http_questioner)
        binder(Of _do_val_ref_ref(Of var, questioner, String, Boolean), 
                  allocate_tcp_questioner_binder_protector).set_global(AddressOf allocate_tcp_questioner)
    End Sub

    Public Function resolve(ByVal name As String,
                            ByVal type As String,
                            ByVal key As String,
                            ByVal o As pointer(Of istrkeyvt)) As event_comb
        Return allocator.istrkeyvt.resolve(name, type, key, o)
    End Function

    Private Function allocate_tcp_questioner(ByVal p As var,
                                             ByRef o As questioner,
                                             ByRef target As String) As Boolean
        assert(Not p Is Nothing)
        Dim is_outgoing As String = Nothing
        Dim host As String = Nothing
        Dim port As String = Nothing
        Dim token As String = Nothing
        Dim max_connecting As String = Nothing
        Dim max_connected As String = Nothing
        Dim no_delay As String = Nothing
        Dim connecting_timeout_ms As String = Nothing
        Dim response_timeout_ms As String = Nothing
        Dim send_rate_sec As String = Nothing
        Dim receive_rate_sec As String = Nothing
        Dim max_lifetime_ms As String = Nothing
        Dim ipv4 As String = Nothing
        Dim enable_keepalive As String = Nothing
        Dim first_keepalive_ms As String = Nothing
        Dim keepalive_interval_ms As String = Nothing
        Dim zipper As String = Nothing
        Dim encryptor As String = Nothing
        Dim pp As powerpoint = Nothing
        If p.parse_tcp_parameters(is_outgoing,
                                  host,
                                  port,
                                  target,
                                  token,
                                  max_connecting,
                                  max_connected,
                                  no_delay,
                                  connecting_timeout_ms,
                                  response_timeout_ms,
                                  send_rate_sec,
                                  receive_rate_sec,
                                  max_lifetime_ms,
                                  ipv4,
                                  enable_keepalive,
                                  first_keepalive_ms,
                                  keepalive_interval_ms,
                                  zipper,
                                  encryptor) AndAlso
           powerpoint.create(token,
                             host,
                             port,
                             ipv4,
                             connecting_timeout_ms,
                             send_rate_sec,
                             response_timeout_ms,
                             receive_rate_sec,
                             max_connecting,
                             max_connected,
                             no_delay,
                             max_lifetime_ms,
                             is_outgoing,
                             enable_keepalive,
                             first_keepalive_ms,
                             keepalive_interval_ms,
                             zipper,
                             encryptor,
                             pp) Then
            o = New questioner(AddressOf pp.question)
            Return True
        Else
            Return False
        End If
    End Function

    Private Function allocate_http_questioner(ByVal p As var,
                                              ByRef o As questioner,
                                              ByRef target As String) As Boolean
        assert(Not p Is Nothing)
        Dim host As String = Nothing
        Dim port As String = Nothing
        Dim connect_timeout_ms As String = Nothing
        Dim response_timeout_ms As String = Nothing
        Dim buff_size As String = Nothing
        Dim single_copy_timeout_ms As String = Nothing
        Dim copy_timeout_ms As String = Nothing
        Dim max_content_length As String = Nothing
        If p.parse_http_parameters(host,
                                   port,
                                   target,
                                   Nothing,
                                   connect_timeout_ms,
                                   response_timeout_ms,
                                   buff_size,
                                   single_copy_timeout_ms,
                                   copy_timeout_ms,
                                   max_content_length) Then
            Dim hq As post_questioner = Nothing
            hq = New post_questioner(host,
                                     port,
                                     connect_timeout_ms,
                                     response_timeout_ms,
                                     buff_size,
                                     single_copy_timeout_ms,
                                     copy_timeout_ms,
                                     max_content_length)
            o = New questioner(+hq)
            Return True
        Else
            Return False
        End If
    End Function
End Module
